﻿namespace ProxyServer.Acme;

public interface IAcmeStore
{
    Task<T?> LoadAsync<T>(string keyFormat, CancellationToken cancellationToken = default, params IEnumerable<object> keyParams);

    Task<T?> LoadRawAsync<T>(string keyFormat, CancellationToken cancellationToken = default, params IEnumerable<object> keyParams);
    Task SaveAsync<T>(T value, string keyFormat, CancellationToken cancellationToken = default, params IEnumerable<object> keyParams);
    Task SaveRawAsync<T>(T value, string keyFormat, CancellationToken cancellationToken = default, params IEnumerable<object> keyParams);

    Task<bool> ExistsAsync(string keyFormat, CancellationToken cancellationToken = default, params IEnumerable<object> keyParams);
    Task RemoveAsync(string keyFormat, CancellationToken cancellationToken = default, params IEnumerable<object> keyParams);
}

public static class AcmeStoreConstants
{
    public static class ObjectKeys
    {
        public const string AcmeAccountDetails = "01_Account";
        public const string AcmeAccountKey = "02_AccountKey";
        public const string AcmeDirectory = "03_Directory";

        public const string AcmeOrder = "04_Order_{0}";

        public const string AcmeOrderAuthz = "04_Order_{0}_Authz_{1}";

        public const string AcmeOrderAuthzChallenge = "04_Order_{0}_Authz_{1}_Challenge_{2}";
        public const string AcmeOrderCertKey = "04_Order_{0}_CertKey";
        public const string AcmeOrderCertCsr = "04_Order_{0}_CertCsr";
        public const string AcmeOrderCert = "04_Order_{0}_Cert";
        public const string PfxFile = "04_Order_{0}_Pfx";
        public const string DomainRecord = "04_Order_{0}_DomainRecord";
    }

    public static class CaNames
    {
        public const string LetsEncrypt = nameof(LetsEncrypt);
        public const string LetsEncryptStaging = nameof(LetsEncryptStaging);
    }

    public static class CaUrls
    {
        public const string LetsEncryptV2StagingEndpoint = "https://acme-staging-v02.api.letsencrypt.org/";
        public const string LetsEncryptV2Endpoint = "https://acme-v02.api.letsencrypt.org/";
    }


    public const string ValidStatus = "valid";
    public const string InvalidStatus = "invalid";
    public const string PendingStatus = "pending";

    public const string RsaKeyType = "rsa";
    public const string EcKeyType = "ec";

    public static IReadOnlyDictionary<string, int> DefaultAlgorKeySizeMap = new Dictionary<string, int>
    {
        { RsaKeyType, 2048  },
        { EcKeyType, 256 }
    };
}